#!/bin/bash
# - repo_name
# - repo_url
# - repo_branch

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/upload.sh

[ -n "$repo_name" ] || die "repo_name is empty"
[ -n "$repo_url" ] || die "repo_url is empty"
[ -n "$repo_branch" ] || die "repo_branch is empty"

workdir=$(pwd)

depends_install() {
    mkdir ${workdir}/qemu_depends/
    sudo yum install -y \
        ninja-build \
        diffutils \
        gcc \
        gcc-c++ \
        git \
        make \
        bison \
        brlapi-devel \
        bzip2-devel \
        chrpath \
        cyrus-sasl-devel \
        device-mapper-multipath-devel \
        flex \
        gettext \
        gnutls-devel \
        gtk3-devel \
        kernel \
        libaio-devel \
        libattr-devel \
        libcap-devel \
        libcap-ng-devel \
        libcurl-devel \
        libepoxy-devel \
        libfdt-devel \
        libiscsi-devel \
        libjpeg-devel \
        libpng-devel \
        librbd-devel \
        libseccomp-devel \
        libtasn1-devel \
        libudev-devel \
        libusbx-devel \
        libxml2-devel \
        lzo-devel \
        ncurses-devel \
        numactl-devel \
        pam-devel \
        perl-Test-Harness \
        perl-podlators \
        pixman-devel \
        python-sphinx \
        python3-devel \
        rdma-core-devel \
        snappy-devel \
        systemd-devel \
        texinfo \
        usbredir-devel \
        virglrenderer-devel \
        zlib-devel \
        libusb-devel \
        xfsprogs-devel \
        device-mapper-devel \
        libgcrypt-devel \
        libasan \
        --downloadonly --downloaddir=${workdir}/qemu_depends
    sudo rpm -ivh --force --nodeps ${workdir}/qemu_depends/*.rpm
}

build() {
    cd ${workdir}
    git clone $repo_url
    cd $repo_name
    git checkout -f $repo_branch
    git submodule update --init ui/keycodemapdb

    if [ "$arch" = "aarch64" ]; then 
        mkdir aarch64-openEuler-linux-gnu
        cd aarch64-openEuler-linux-gnu
        target_list="--target-list=aarch64-softmmu"
    else
        mkdir x86_64-openEuler-linux-gnu
        cd x86_64-openEuler-linux-gnu
        target_list="--target-list=x86_64-softmmu" 
    fi

    if grep -qw -- "--disable-bluez" ../configure; then
        disable_bluez="--disable-bluez"
    else
        disable_bluez=""
    fi

    if [ x$san == x"enable" ];then
        enable_san="--enable-sanitizers"
        concurrency=60
    else
        enable_san=""
        concurrency=$(getconf _NPROCESSORS_ONLN)
    fi

    ../configure \
        --prefix=/usr \
        ${target_list} \
        '--extra-cflags=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/openEuler/openEuler-hardened-cc1 -fasynchronous-unwind-tables -fstack-clash-protection -fPIE -DPIE -fPIC -Wno-error' \
        '--extra-ldflags=-Wl,--build-id -pie -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack' \
        --datadir=/usr/share \
        --docdir=/usr/share/doc/qemu \
        --libdir=/usr/lib64 \
        --libexecdir=/usr/libexec \
        --localstatedir=/var \
        --sysconfdir=/etc \
        --interp-prefix=/usr/qemu-%M \
        --firmwarepath=/usr/share/qemu \
        --with-pkgversion=qemu-4.1.0-17.oe1 \
        --python=/usr/bin/python3 \
        --disable-strip \
        --disable-slirp \
        --enable-gtk \
        --enable-docs \
        --enable-guest-agent \
        --enable-pie \
        --enable-numa \
        --enable-mpath \
        --disable-libnfs \
        --disable-bzip2 \
        --enable-kvm \
        --enable-linux-aio \
        --enable-cap-ng \
        --enable-vhost-user \
        --enable-fdt \
        --enable-virglrenderer \
        --enable-libusb \
        ${disable_bluez} \
        --disable-dmg \
        --disable-qcow1 \
        --disable-vdi \
        --disable-vvfat \
        --disable-qed \
        --disable-parallels \
        --disable-sheepdog \
        --disable-capstone \
        --disable-smartcard \
        ${enable_san} \
        || (cat config.log; exit 1)

    make -j ${concurrency} VL_LDFLAGS=-Wl,--build-id V=1
    make check V=1
}

cppcheck() {
    cd ${workdir}
    cppcheck_repo="https://gitee.com/mirrors/cppcheck.git"
    cppcheck_options=${cppcheck_option:-"--inconclusive --xml --xml-version=2 CSLB"}
    git clone $cppcheck_repo
    cd cppcheck
    make -j 60 CFGDIR=/usr/local/cppcheck/cfg FILESDIR=/usr/share/cppcheck
    make install FILESDIR=/usr/share/cppcheck
    ulimit -n 10240
    chmod 777 /usr/bin/cppcheck
    /usr/bin/cppcheck -j 60 ${workdir}/${repo_name} $cppcheck_options 2> ${workdir}/cppcheck.xml

    upload_files -t results ${workdir}/cppcheck.xml
}

depends_install
build
if [ x$cppcheck == x"enable" ]; then
    cppcheck
fi
